﻿using UnityEngine;
using System.Collections;

public class SecurityCameraManager : MonoBehaviour {

    public float maxRotation = 20; // Maximum rotation of camera in degrees Y
    public float minRotation = -20;  // Minimum rotation of camera in degrees Y

    private float currentRotation = 0.0f;
    private bool movingForward = true;

    public float pauseTime = 2.0f;
    public float sweepTime = 4.0f;
    public float focusTime = 2.0f;

    public float scanDistance = 4.0f;

    public bool debug = false;

    private float currentPauseTime = 0.0f;

    private ScanLineManager scanLineManager;
    private Vector3 toPlayer;
    private float angleToPlayer;

	// Use this for initialization
	void Start () {
        currentPauseTime = pauseTime;
        scanLineManager = transform.FindChild("ScanningLine").GetComponent<ScanLineManager>();

        scanLineManager.scanDistance = this.scanDistance;
	}
	
	// Update is called once per frame
	void Update () {

        GameObject player = GameObject.FindGameObjectWithTag("Player");

        toPlayer = player.transform.position - transform.position;
        toPlayer.z = 0;
        angleToPlayer = Vector3.Angle(transform.right, toPlayer);

        Quaternion testRotation = Quaternion.AngleAxis(angleToPlayer, transform.forward);
        Vector3 rotatedLook = testRotation * transform.right;

        if (Vector3.Angle(toPlayer, rotatedLook) > angleToPlayer)
        {
            angleToPlayer *= -1;
        }


        if (!CanSeePlayer())
        {
            if (debug) print("Can't see player");
            scanLineManager.currentArcSize = Mathf.Lerp(0, scanLineManager.arcSize, scanLineManager.currentArcSize + Time.deltaTime / focusTime);
            DoSurveillance();
        }
        else
        {
            if (debug) print("Can see player, angle to player is " + angleToPlayer);
            scanLineManager.currentArcSize = Mathf.Lerp(scanLineManager.arcSize * 0.1f, scanLineManager.arcSize, (scanLineManager.currentArcSize - scanLineManager.arcSize * 0.1f) / (scanLineManager.arcSize * 0.9f) - Time.deltaTime / focusTime);
            if (scanLineManager.currentArcSize <= scanLineManager.arcSize * 0.1f)
            {
                player.GetComponent<DeathDetection>().Electrocuted();
            }

            float angleChange = Mathf.Lerp(0, angleToPlayer, Time.deltaTime / focusTime * 2);
            currentRotation += angleChange;
            if (debug) print("Angle change is " + angleChange);
            transform.Rotate(transform.forward, angleChange);

        }
    }

    private bool CanSeePlayer()
    {
        if (Mathf.Abs(angleToPlayer) < scanLineManager.currentArcSize)
        {
            // The player is in the arc; check a raycast
            RaycastHit hitInfo;
            if (Physics.Raycast(transform.position, toPlayer, out hitInfo, scanDistance))
            {
                if (hitInfo.collider.tag == "Player")
                {
                    return true;
                }
            }
        }
        return false;
    }

    private void DoSurveillance()
    {
        if (currentPauseTime < pauseTime)
        {
            currentPauseTime += Time.deltaTime;
        }
        else
        {
            if (maxRotation != minRotation)
            {
                float lastRotation = currentRotation;
                if (movingForward)
                {
                    currentRotation = Mathf.Lerp(minRotation, maxRotation, (currentRotation - minRotation) / (maxRotation - minRotation) + Time.deltaTime / sweepTime);
                    if (currentRotation >= maxRotation)
                    {
                        movingForward = false;
                        currentPauseTime = 0.0f;
                    }
                }
                else
                {
                    currentRotation = Mathf.Lerp(minRotation, maxRotation, (currentRotation - minRotation) / (maxRotation - minRotation) - Time.deltaTime / sweepTime);
                    if (currentRotation <= minRotation)
                    {
                        movingForward = true;
                        currentPauseTime = 0.0f;
                    }
                }

                transform.Rotate(transform.forward, currentRotation - lastRotation);
            }

        }
    }
}
